package com.football.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.football.springboot.common.Constants;
import com.football.springboot.entity.Dict;
import com.football.springboot.entity.Season;
import com.football.springboot.mapper.SeasonMapper;
import com.football.springboot.vo.LeagueQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.football.springboot.common.Return;
import com.football.springboot.common.Result;
//导入service
import com.football.springboot.service.ILeagueService;
//导入entity
import com.football.springboot.entity.League;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李越
 * @since 2023-02-17
 */
@Api(tags = "联赛管理")
@RestController
@RequestMapping("/league")
public class LeagueController {

    @Resource
    private ILeagueService leagueService;

    @Resource
    private SeasonMapper seasonMapper;
    // 新增或者更新
    @ApiOperation("新增或者更新")
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody League league) {
        leagueService.saveOrUpdate(league);
        return Result.success();
    }


    //删除
    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        leagueService.removeById(id);
        return Result.success();
    }

    //批量删除
    @ApiOperation("批量删除")
    @PostMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        leagueService.removeByIds(ids);
        return Result.success();
    }

    //查询全部
    @ApiOperation("查询全部")
    @GetMapping("/findall")
    public Result findAll() {
        return Result.success(leagueService.list());
    }

    //根据id查找
    @ApiOperation("根据id查找")
    @GetMapping("/findOne/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(leagueService.getById(id));
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/pageLeague")
    public Return pageLeague(@RequestParam Integer current,
                             @RequestParam Integer limit,
                             LeagueQueryVo leagueQueryVo) {

        //创建page对象,传递当前页数current,每页记录数limit
        Page<League> page = new Page<>(current,limit);
        //构建条件 模糊查询
        QueryWrapper<League> queryWrapper = new QueryWrapper<>();

        String leagueName = leagueQueryVo.getLeagueName();
        String leagueSeasn = leagueQueryVo.getLeagueSeason();

        //联赛名称查询
        if(!StringUtils.isEmpty(leagueName)){
            queryWrapper.like("league_name",leagueName);
        }
        //联赛赛季查询
        if(!StringUtils.isEmpty(leagueSeasn)){
            queryWrapper.like("league_season",leagueSeasn);
        }

        queryWrapper.orderByDesc("id");
        Page<League> pageLeague = leagueService.page(page,queryWrapper);

        return Return.ok().put("pageLeague",pageLeague).put("total",pageLeague.getTotal());
    }

    //获取赛季值
    @ApiOperation("获取赛季值")
    @GetMapping("/season")
    public Result getSeason(){
        QueryWrapper<Season> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.SEASON_TYPE);
        return Result.success(seasonMapper.selectList(queryWrapper));
    }

    //编辑该联赛的参赛球队
    @ApiOperation("编辑该联赛的参赛球队")
    @PostMapping("/leagueTeam/{leagueId}")
    public Result leagueTeam(@PathVariable Integer leagueId,@RequestBody List<Integer> teamIds){
        leagueService.setLeagueTeam(leagueId,teamIds);
        return Result.success();
    }


    //查看该联赛的参赛球队id
    @ApiOperation("查看该联赛的参赛球队id")
    @GetMapping("/leagueTeam/{leagueId}")
    public Result getLeagueTeam(@PathVariable Integer leagueId){
        return Result.success(leagueService.getLeagueTeam(leagueId));
    }

    //查看该联赛的参赛球队team
    @ApiOperation("查看该联赛的参赛球队id")
    @GetMapping("/findTeamByLeagueId/{leagueId}")
    public Result findTeamByLeagueId(@PathVariable Integer leagueId){
        return Result.success(leagueService.findTeamByLeagueId(leagueId));
    }

    //前台获取联赛信息
    @ApiOperation("前台获取联赛信息")
    @GetMapping("/front/findOne/{leagueId}")
    public Result frontfindone(@PathVariable Integer leagueId) {
        return Result.success(leagueService.getById(leagueId));
    }



}

